package com.nanychuan.blogback.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.nanychuan.blogback.pojo.Article;
import com.nanychuan.blogback.pojo.Category;
import com.nanychuan.blogback.pojo.PageBean;
import com.nanychuan.blogback.pojo.User;
import com.nanychuan.blogback.service.BlogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author 往
 * 博客前台文章展示
 */
@Controller
public class BlogController {
    @Autowired
    private BlogService blogService;
    //查询所有后台文章，含分页功能
    @RequestMapping("/blog/selectAllArticle")
    @ResponseBody
    public PageInfo<Article> blogList(int page,int pageSize,String title,HttpSession session ){
        //获取当前登录用户
        User user = (User) session.getAttribute("user");
        //参数1:当前页码 参数2:每页记录数 pageSize 该方法等同于 limit a,b
        PageHelper.startPage(page,pageSize);
        List<Article> articles = blogService.selectAll(user.getUid(),title);
        PageInfo<Article> pageInfo = new PageInfo<>(articles);
        return pageInfo;
    }

    //查询所有前端文章，不含分页功能
    @RequestMapping("/blog/selectAllPreArticle")
    @ResponseBody
    public List<Article> articleList(){
        //查询文章
        List<Article> blogArticles = blogService.selectAllPreArticle();
        return blogArticles;
    }

    //查询文章标题
    @RequestMapping("/blog/selectTitle")
    @ResponseBody
    public List<Category> categoryList(){
        //查询文章标题
        List<Category> categories = blogService.selectCategory();
        return categories;
    }

    //前端分页查询功能
    @RequestMapping("/blog/pageSelect")
    @ResponseBody
    public PageInfo<Article> pageSelect(int page,int pageSize){
        //参数1:当前页码 参数2:每页记录数 pageSize 该方法等同于 limit a,b
        PageHelper.startPage(page,pageSize);
        List<Article> articles = blogService.selectAllPreArticle();
        PageInfo<Article> pageData = new PageInfo<>(articles);
        return pageData;
    }
}
